//
//  BUYMyGoodsControl.m
//  YiFei
//
//  Created by yons on 16/8/27.
//  Copyright © 2016年 yous. All rights reserved.
//

#import "MyProductionController.h"
#import "ZMJNewProductionController.h"
#import "ZMJMPHeaderView.h"
#import "ZMJMyProductionCell.h"
#import "ZMJEditViewController.h"
#import "ZMJGoodsDetailController.h"


@interface MyProductionController ()<UISearchResultsUpdating,UISearchBarDelegate,ZMJMyProductionCellDelegate,UITableViewDelegate,UITableViewDataSource,UISearchControllerDelegate>{
    
    ZMJMyProductionCell *cell;
    //搜索结果数组
    NSArray *_searchArray;
    UIView *backView;
//    NSMutableArray *temArray;
    
}
//保存的数据
@property(nonatomic,strong)NSMutableArray *listArray;
//表格视图
@property(nonatomic,strong)UITableView *tableview;
//searchController
@property(nonatomic,strong)UISearchController *searchC;
//二进制数据流
@property(nonatomic,strong)NSData *imgData;


@property(nonatomic,strong)NSArray *arrarYM;

@end

@implementation MyProductionController

-(void)viewWillAppear:(BOOL)animated{
 [super viewWillAppear:animated];
   [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self createNavigationView];
    [self addContentView];
 
}

-(void)loadData{
    //获取单例对象
    FMDBOneList  *manager = [FMDBOneList defaultManager];
    //可变数组初始化
    _listArray = [NSMutableArray arrayWithArray:[manager getData]];
    [_tableview reloadData];
}


#pragma mark 创建navgationView
-(void)createNavigationView
{
    self.navigationItem.title = @"我的商品";
    BackButton *leftBtn = [[BackButton alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"fanhui_icon"] forState:UIControlStateNormal];
    UIBarButtonItem * barItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = barItem;
    [leftBtn addTarget:self action:@selector(leftButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * rightButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rightButtonClick:)];
    self.navigationItem.rightBarButtonItem=rightButton;

}

-(void)leftButtonClick{
    [self.navigationController popViewControllerAnimated:YES];
    _searchC.dimsBackgroundDuringPresentation=YES;
}


-(void)addContentView{
    
    backView=[[UIView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, 44)];
    [self.view addSubview:backView];
    _tableview=[[UITableView alloc] initWithFrame:CGRectMake(5, 108+10, WIDTH-10, HEIGHT-108-10)style:UITableViewStyleGrouped];
    self.tableview.backgroundColor=[UIColor whiteColor];
    self.tableview.rowHeight=100;
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.tableHeaderView.layer.masksToBounds=YES;
    self.tableview.tableHeaderView.layer.cornerRadius=5.0;
    [self.view addSubview:self.tableview];
    //初始化搜索栏
    _searchC = [[UISearchController alloc] initWithSearchResultsController:nil];
    _searchC.searchBar.barTintColor=[UIColor colorWithRed:234/255.0 green:234/255.0 blue:234/255.0 alpha:1.0];
    _searchC.delegate=self;
    //是否隐藏导航
    _searchC.hidesNavigationBarDuringPresentation = NO;
    _searchC.dimsBackgroundDuringPresentation=NO;
    _searchC.searchBar.barStyle=UIBarStyleBlack;
    //自适应
    [_searchC.searchBar sizeToFit];
    //提示信息
    _searchC.searchBar.placeholder = @"请输入商品名称";
    _searchC.searchBar.showsCancelButton=YES;
    UIButton *canceLBtn = [_searchC.searchBar valueForKey:@"cancelButton"];
    [canceLBtn setTitle:@"取消" forState:UIControlStateNormal];
    [canceLBtn setTitleColor:NAVCOLOR forState:UIControlStateNormal];
    //代理方法
    _searchC.searchResultsUpdater = self;
    _searchC.delegate=self;
    [backView addSubview:_searchC.searchBar];
    self.definesPresentationContext=YES;
}



#pragma mark -- 搜索方法
// 搜索时触发的方法
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
//    _searchArray=[readValueData getResultOfSearch:searchController.searchBar.text];
    // 刷新列表
    [self.tableview reloadData];
}



#pragma Mark -- 事件处理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_searchC.active) {
        return _searchArray.count;
    }
    return _listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdent=@"cell";
    cell=[tableView dequeueReusableCellWithIdentifier:cellIdent];
    if (cell==nil) {
        cell=[[ZMJMyProductionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];
    }
    shopData *dataModel = [[shopData alloc] init];
    
    if (_searchC.active) {
        //赋值对应的对象
    dataModel = _searchArray[indexPath.row];
    }else{
    dataModel = _listArray[indexPath.row];
    }
    NSArray *arrayimg=[dataModel.shopPicture componentsSeparatedByString:@"|"];
    NSString *path_document = NSHomeDirectory();
    //设置一个图片的存储路径
    NSString *imagePath = [path_document stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@.png",arrayimg[0]]];
    cell.imgV.image= [UIImage imageWithContentsOfFile:imagePath];
    cell.nameShop.text=[NSString stringWithFormat:@"%@",dataModel.shopName];
    cell.priceL.text=[NSString stringWithFormat:@"￥%@",dataModel.shopPrice];
    cell.delegate = self;
    return cell;
}


#pragma mark 点击页面进行跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark 创建头部视图

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    ZMJMPHeaderView *headerView = [[ZMJMPHeaderView alloc] init];
    headerView.numLabel.text = [NSString stringWithFormat:@"%ld",_listArray.count];
    return headerView;
}


#pragma mark 创建新的商品
-(void)rightButtonClick:(UIButton *)button{
    _searchC.dimsBackgroundDuringPresentation=YES;
    ZMJNewProductionController *shopVC=[[ZMJNewProductionController alloc] init];
    [self.navigationController pushViewController:shopVC animated:YES];
}

#pragma mark 跳转到详情界面
- (void)clickView:(ZMJGoodsDetailController *)controller index:(UITableViewCell *)index
{
    NSIndexPath *indexPath =  [_tableview indexPathForCell:index];
    NSInteger  row = indexPath.row;
ZMJGoodsDetailController *detailPriceVC=[[ZMJGoodsDetailController alloc] init];
    detailPriceVC.shopData=_listArray[row];
    [self.navigationController pushViewController:detailPriceVC animated:YES];
}

#pragma mark 跳转到编辑界面
- (void)clickLabelView:(ZMJEditViewController *)controller index:(UITableViewCell *)index
{
    NSIndexPath *indexPath =  [_tableview indexPathForCell:index];
    NSInteger  row = indexPath.row;
   ZMJEditViewController *detailPVC=[[ZMJEditViewController alloc] init];
    if (_searchC.active) {
        //赋值对应的对象
    detailPVC.shopObj = _searchArray[row];
    }else{
    detailPVC.shopObj = _listArray[row];
    }
[self.navigationController pushViewController:detailPVC animated:YES];
}


//设置编辑cell的样式（可省略）
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPat{
    return UITableViewCellEditingStyleDelete;
}
//实现删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FMDBOneList *manager = [FMDBOneList defaultManager];
    shopData *model = _listArray[indexPath.row];
    [manager deleteNameFromTable:model.companyID];
    //2.
    [_listArray removeObjectAtIndex:indexPath.row];
    //3
    //[_tableView reloadData];
    //刷新其中一个或几个cell。不是全部刷新
    //[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [_tableview reloadData];
}











@end













